package com.maple.common.widget.label

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PointF
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import androidx.annotation.Dimension
import androidx.annotation.IntDef
import androidx.core.content.withStyledAttributes
import com.maple.common.R
import com.maple.common.log.LogUtil
import com.maple.common.widget.label.LabelView.LabelLocation.Companion.BOTTOM_LEFT
import com.maple.common.widget.label.LabelView.LabelLocation.Companion.BOTTOM_RIGHT
import com.maple.common.widget.label.LabelView.LabelLocation.Companion.TOP_LEFT
import com.maple.common.widget.label.LabelView.LabelLocation.Companion.TOP_RIGHT

/**
 * Tip:标签
 * Create by SeVen on 2025/2/13 10:59
 */
class LabelView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    /**
     * 标签的位置
     */
    @LabelLocation
    private var labelLocation: Int = TOP_RIGHT

    /**
     * 默认宽度为0
     */
    @Dimension(unit = Dimension.DP)
    private var unitWidth: Float = 0f

    /**
     * 默认标签短边线距离各定点的距离
     */
    @Dimension(unit = Dimension.DP)
    private var distance: Float = 0f

    /**
     * 默认标签颜色为红色
     */
    @ColorInt
    private var labelBackground: Int = Color.RED

    /**
     * 默认标签文字大小为12sp
     */
    @Dimension(unit = Dimension.SP)
    private var labelTextSize: Float = 12f

    /**
     * 默认标签文字颜色为白色
     */
    @ColorInt
    private var labelTextColor: Int = Color.WHITE

    /**
     * 默认标签文本
     */
    private var labelText: String? = null

    /**
     * 标签画笔
     */
    private val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    /**
     * 标签路径
     */
    private val labelPath = Path()

    /**
     * 标签文字画笔
     */
    private val labelTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        initAttrs(context, attrs)
        initPaint()
    }

    /**
     * 初始化自定义属性
     */
    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        context.withStyledAttributes(attrs, R.styleable.LabelView) {
            labelLocation = getInteger(R.styleable.LabelView_lv_location, TOP_RIGHT)
            unitWidth = getDimension(R.styleable.LabelView_lv_unitWidth, unitWidth)
            labelBackground = getColor(R.styleable.LabelView_lv_background, labelBackground)
            distance = getDimension(R.styleable.LabelView_lv_distance, distance)

            labelText = getString(R.styleable.LabelView_lv_text)
            labelTextSize = getDimension(R.styleable.LabelView_lv_textSize, labelTextSize)
            labelTextColor = getColor(R.styleable.LabelView_lv_textColor, labelTextColor)
        }
    }

    /**
     * 初始化画笔
     */
    private fun initPaint() {
        labelPaint.apply {
            color = labelBackground
            style = Paint.Style.FILL
        }

        labelTextPaint.apply {
            color = labelTextColor
            textSize = labelTextSize
            textAlign = Paint.Align.CENTER
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        LogUtil.e("============${width}=====================$height")
        labelPaint.setColor(Color.RED)
        canvas.drawCircle(100f,100f,100f,labelPaint)
        // 清空路径
        labelPath.reset()

        // 动态计算标签位置
        val width = if (unitWidth > 0) unitWidth else measuredWidth.toFloat()
        val height = width / 2 // 假设标签为等腰三角形
        val pathPoints = calculateLabelPath(width, height)

        // 设置路径
        labelPath.moveTo(pathPoints[0].x, pathPoints[0].y)
        for (i in 1 until pathPoints.size) {
            labelPath.lineTo(pathPoints[i].x, pathPoints[i].y)
        }
        labelPath.close()

        // 绘制路径
        canvas.drawPath(labelPath, labelPaint)

        // 绘制文字
        labelText?.let { text ->
            val textX = when (labelLocation) {
                TOP_LEFT -> distance + width / 2
                TOP_RIGHT -> measuredWidth - distance - width / 2
                BOTTOM_LEFT -> distance + width / 2
                BOTTOM_RIGHT -> measuredWidth - distance - width / 2
                else -> width / 2
            }
            val textY = when (labelLocation) {
                TOP_LEFT, TOP_RIGHT -> distance + height / 2
                BOTTOM_LEFT, BOTTOM_RIGHT -> measuredHeight - distance - height / 2
                else -> height / 2
            }
            canvas.drawText(text, textX, textY, labelTextPaint)
        }
    }

    /**
     * 根据宽度和高度计算标签路径的顶点
     */
    private fun calculateLabelPath(width: Float, height: Float): List<PointF> {
        return when (labelLocation) {
            TOP_LEFT -> listOf(
                PointF(0f, 0f),
                PointF(width, 0f),
                PointF(0f, height)
            )
            TOP_RIGHT -> listOf(
                PointF(measuredWidth.toFloat(), 0f),
                PointF(measuredWidth - width, 0f),
                PointF(measuredWidth.toFloat(), height)
            )
            BOTTOM_LEFT -> listOf(
                PointF(0f, measuredHeight.toFloat()),
                PointF(width, measuredHeight.toFloat()),
                PointF(0f, measuredHeight - height)
            )
            BOTTOM_RIGHT -> listOf(
                PointF(measuredWidth.toFloat(), measuredHeight.toFloat()),
                PointF(measuredWidth - width, measuredHeight.toFloat()),
                PointF(measuredWidth.toFloat(), measuredHeight - height)
            )
            else -> emptyList()
        }
    }

    /**
     * 定义标签位置的注解
     */
    @Retention(AnnotationRetention.SOURCE)
    @IntDef(TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT)
    annotation class LabelLocation {
        companion object {
            const val TOP_LEFT = 0
            const val TOP_RIGHT = 1
            const val BOTTOM_LEFT = 2
            const val BOTTOM_RIGHT = 3
        }
    }
}